#!/bin/sh
# the next line restarts using wish \
exec wish "$0" ${1+"$@"}

# Loader --
# Simple Tk script for loading propeller with propeller-load.exe
# Click the Browse button to find a program to load.
# Click the Load button to load a binary program to ram and start.
# Click the Burn button to load a binary program to eeprom - start with chip reset.
# Click the Quit button to exit.

package require Tk

proc getResult { result } {
    set output ""
    foreach item $result {
        if {[regexp "Ver" $item]} {
            append output $item "\n"
        }
        if {[regexp "sent" $item]} {
            append output $item "\n"
        }
    }
    return $output
}

proc program { thefile loadopt loaddst } {
    set status 0
    #puts "$thefile $loadopt $loaddst"
    if {[catch {exec -ignorestderr ./propeller-load $loadopt "$thefile"} result options]} {
        set result [split $result "\n"]
        tk_messageBox -message "Error loading $thefile\n\n[lindex $result 0]"
    } else {
        set result [split $result "\n"]
        set output [getResult $result]
        tk_messageBox -message "$thefile\nLoaded $loaddst\n\n$output"
    }
}

set width   550
set height  120

set x [expr { ( [winfo vrootwidth  .] - $width) / 2 }]
set y [expr { ( [winfo vrootheight .] - $height) / 2 }]
wm geometry . ${width}x${height}+${x}+${y}

frame .pnl
label .pnl.help -text "Select a Propeller .binary, .eeprom, or .elf file." -padx 8 -pady 8
text  .pnl.txt  -width 55 -height 1 -padx 8 -xscrollcommand { .pnl.txt.s set }
scrollbar .pnl.txt.s -orient horizontal -command { .pnl.txt xview }

button .pnl.browse -text "Browse" -command {
    set types [list]
    lappend types "typeName .binary"
    lappend types "typeName .eeprom"
    lappend types "typeName .elf"
    set thefile [tk_getOpenFile -filetypes $types]
    if {[string length $thefile] > 0} {
        .pnl.txt delete 1.0 end
        .pnl.txt insert end $thefile
    }
}

button .burn -text "Burn" -command {
    set err 0
    set thefile [string trim [.pnl.txt get 1.0 end]]

    set loadopt "-e"
    if { [regexp ".binary" $thefile] } {
        set loaddst "to EEPROM"
    } elseif { [regexp ".eeprom" $thefile] } {
        set loaddst "to EEPROM"
    } elseif { [regexp ".elf" $thefile] } {
        set loaddst "ELF as BINARY to EEPROM"
        if {[catch {exec -ignorestderr ./propeller-load -s $thefile} result options]} {
            tk_messageBox -message $result
        }
    } else {
        tk_messageBox -message "File must be type .binary, .eeprom or .elf";
        set err 1
    }

    if {0 == $err} {
        program $thefile $loadopt $loaddst
    }
}

button .load -text "Load" -command {
    set err 0
    set thefile [string trim [.pnl.txt get 1.0 end]]

    set loadopt "-r"
    if { [regexp ".binary" $thefile]} {
        set loaddst "to RAM"
    } elseif { [regexp ".eeprom" $thefile] } {
        set loaddst "to EEPROM"
    } elseif { [regexp ".elf" $thefile] } {
        set loaddst "ELF as BINARY to RAM"
        if {[catch {exec -ignorestderr ./propeller-load -s $thefile} result options]} {
            tk_messageBox -message $result
        }
    } else {
        tk_messageBox -message "File must be type .binary, .eeprom, or .elf";
        set err 1
    }

    if {0 == $err} {
        program $thefile $loadopt $loaddst
    }
}

button .quit -text "Quit" -command {
    destroy .
}


pack .pnl
pack .pnl.help      -side top
pack .pnl.txt       -side left -padx 8 -pady 8
pack .pnl.browse    -side left -padx 8 -pady 8

pack .quit -side right -padx 4 -pady 8
pack .load -side right -padx 4 -pady 8
pack .burn -side right -padx 4 -pady 8

#wm iconbitmap . -default "Loader.ico"
wm title . "Simple Propeller Loader"
wm resizable . 0 0
# End:
